import { dictTypeList } from './data/dict.type'
import Mock from 'mockjs'
import { isDateTimeInRange, getNowDateTime } from '../../utils'
export default [
  {
    url: '/system/dict-type/create', // 新增
    method: 'post',
    response: data => {
      insert(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: '/system/dict-type/update', // 修改
    method: 'put',
    response: data => {
      updateById(data)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/dict-type/delete' + '.*'), // 删除
    method: 'delete',
    response: params => {
      deleteById(params)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/dict-type/detail' + '.*'), // 获取详情
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getInfoById(params)
      }
    }
  },
  {
    url: RegExp('/system/dict-type/page' + '.*'), // 查询字典分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/system/dict-type/simple-list' + '.*'), // 查询字典简化列表
    method: 'get',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListBySimple()
      }
    }
  },
  {
    url: RegExp('/system/dict-type/export' + '.*'), // 导出字典类型
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByExport(params)
      }
    }
  }
]

const getListByPage = params => {
  let result = dictTypeList
  result = result.filter(item => !item.deleted)
  if (params.query.status) {
    result = result.filter(item => item.status === parseInt(params.query.status))
  }
  if (params.query.name) {
    result = result.filter(item => item.name.indexOf(params.query.name) !== -1)
  }
  if (params.query.type) {
    result = result.filter(item => item.type.indexOf(params.query.type) !== -1)
  }
  if (params.query.create_time && params.query.create_time.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.create_time[0], params.query.create_time[1], item.create_time))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getListBySimple = () => {
  return dictTypeList.filter(item => item.status === 0 && !item.deleted).map(item => ({ id: item.id, name: item.name, type: item.type }))
}

const getListByExport = params => {
  let result = dictTypeList
  result = result.filter(item => !item.deleted)
  if (params.query.status) {
    result = result.filter(item => item.status === parseInt(params.query.status))
  }
  if (params.query.name) {
    result = result.filter(item => item.name.indexOf(params.query.name) !== -1)
  }
  if (params.query.type) {
    result = result.filter(item => item.type.indexOf(params.query.type) !== -1)
  }
  if (params.query.create_time && params.query.create_time.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.create_time[0], params.query.create_time[1], item.create_time))
  }
  return result
}

const insert = data => {
  dictTypeList.unshift({
    ...data.body,
    id: Mock.Random.id(),
    creator: '1',
    create_time: getNowDateTime(),
    updater: '',
    update_time: '',
    deleted: false,
    deleted_time: ''
  })
}

const updateById = data => {
  dictTypeList.forEach(item => {
    if (item.id === data.body.id) {
      Object.keys(data.body).forEach(v => {
        item[v] = data.body[v]
      })
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}

const deleteById = params => {
  dictTypeList.forEach(item => {
    if (item.id === params.query.id) {
      item.deleted = true
      item.updater = '1'
      item.update_time = getNowDateTime()
      item.deleted_time = getNowDateTime()
    }
  })
}

const getInfoById = params => {
  const result = dictTypeList.filter(item => {
    return item.id === params.query.id
  })
  if (result.length > 0) {
    return result[0]
  } else {
    return null
  }
}
